package com.northpool.operator.statistics.dataset;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.alibaba.fastjson.JSON;
import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.annotation.OutputParam;
import com.northpool.operator.annotation.OutputParams;
import com.northpool.operator.common.algorithm.OperatorTestBuild;
import com.northpool.operator.common.algorithm.TestParamBuilder;
import com.northpool.operator.common.database.DatabaseUtils;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.type.TypeEnum;
import com.northpool.operator.utils.StpOperatorHelper;
import com.northpool.resources.datasource.IDataSource;
import com.northpool.resources.datasource.db.DbDataSource;

import java.util.List;
import java.util.Map;

@OperatorInfo(
        ref = "StatisticalData4Sql",
        name = "通过sql获取对象结果"
)
@OutputParams({
        @OutputParam(ref = "sqlResults", name = "Sql统计结果", type = TypeEnum.STRING)
})
public class StatisticalData4Sql  extends StpOperatorHelper {

    @InputParams({
            @InputParam(ref = "dataSourceId", name = "数据源ID", desc = "数据源ID", type = TypeEnum.DATASOURCE, required = true),
            @InputParam(ref = "sql", name = "SQL语句", desc = "完整的SQL语句，末尾不要加';'", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "resultType", name = "返回数组对象", desc = "是否返回数组对象", type = TypeEnum.BOOL, required = true)
    })
    public StatisticalData4Sql() {

    }

    public static void main(String[] args) throws Exception {
        OperatorTestBuild.execute(
                StatisticalData4Sql.class,
                TestParamBuilder.aBuilder()
                        .param("dataSourceId", "402881ac93c476360193cb4d554a02fd")
                        .param("sql","select 1")
                        .build()
        );
    }

    @Override
    public void execute(ParamTool paramTool) throws Exception {
        this.log("开始执行统计任务...");
        String dataSourceId = paramTool.getStr("dataSourceId");
  
//        String dataSourceId = dataset.getDataSourceId();
//        String tableName = dataset.getTableName();

        String dataSourceStr = DatabaseUtils.getDataSourceJson(dataSourceId);
        DbDataSource datasource = null;
        try {
            datasource = (DbDataSource)IDataSource.fromJSON(dataSourceStr);
            String strSql= paramTool.getStr("sql");
            Boolean bResultType= paramTool.getBool("resultType");

            List<Map<String, Object>> maps = datasource.queryBySql(strSql, null);
            if(!maps.isEmpty()) {
                if(bResultType)
                {
                    this.result("sqlResults", JSON.toJSONString(maps));
                }
                else
                {
                    this.result("sqlResults", JSON.toJSONString(maps.get(0)));
                }
            }

            this.successExit();
        } catch (Exception ex) {
            if (datasource != null) {
                datasource.destroy();
            }
            this.error(ExceptionUtil.getMessage(ex));
            this.failedExit();
        }
    }
}
